#include <iostream>
#include <vector>
using namespace std;
/*
    如果push的和pop的最后一个相同就是栈，反之就是队列
*/
int main()
{
    int t, n, temp, num;
    vector<int> pop_, push_;
    cin >> t;
    for (size_t i = 0; i < t; i++)
    {
        cin >> n;
        for (size_t j = 0; j < n; j++)
        {
            cin >> temp;
            if (temp == 1)
            {
                cin >> num;
                push_.push_back(num);
            }
            else
            {
                cin >> num;
                pop_.push_back(num);
            }
        }
        if (pop_ == push_)
        {
            cout << "Queue" << endl;
        }
        else
            cout << "Stack" << endl;
        pop_.clear();
        push_.clear();
    }

    return 0;
}